<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class SubjectsApiController extends AbstractRestfulController
{
    public function getList()
    {			
		header('Access-Control-Allow-Origin: *');
		$courseTable=$this->getServiceLocator()->get('Models\Model\SubjectsFactory');
		$subjectList = $courseTable->getTotalSubjectList();
		if(($subjectList->count())!=0){
			$data = array();$i=0;
			foreach($subjectList as $subjects){
				$sid=$subjects->subject_id;
				$cid=$subjects->course_id;
				$data[$i]['sno']=$i+1;
				$data[$i]['subject_name']= $subjects->subject_name;
				$data[$i]['course_name']= $subjects->course_name;
				$data[$i]['action']='<a href="javascript:void(0)" onclick = "editSubjectFunction('.$sid.','.$cid.')">Edit</a> &nbsp;/&nbsp;<a href="javascript:void(0);" onClick="deleteSubject('.$sid.')">Delete</a>';
				$i++;
			}
			$data['aaData'] = $data;
			echo json_encode($data['aaData']); exit;
		}else{
			return new JsonModel(array(
				'output' 	=> 'no data',
				'value'		=>	0,
			));
		}	
    }
    public function get($subject_id)
    {		
		header('Access-Control-Allow-Origin: *');	
		$subjectTable=$this->getServiceLocator()->get('Models\Model\SubjectsFactory');
		$exploredId=explode('-',$subject_id);
		$subject_id=$exploredId[0];
		$value=$exploredId[1];
		$course_id=$exploredId[2];
		if($value==0){
			$subjectList = $subjectTable->getSubjectList($subject_id)->toarray();
		}else if($value==2){
			$subjectList = $subjectTable->courseSubjects($course_id)->toarray();
		}else{
			$subjectList = $subjectTable->getSubject($subject_id,$course_id)->toarray();
		}
		if(count($subjectList)!=0){
			return new JsonModel(array(
				'Subjectlist'   => json_encode($subjectList),
				'value' 		=> 1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'  => 0,
			));
		}
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');	
		$subjectTable=$this->getServiceLocator()->get('Models\Model\SubjectsFactory');
		if($data['SubId']==""){
			$saveSubject = $subjectTable->saveSubjects($data);
		}else{
			$saveSubject = $subjectTable->updateSubjects($data);
		}
		return new JsonModel(array(
				'output' 	=> 	'success',
				'subjectId'	=>	$saveSubject,
				'value' 	=> 	1,
			));		
    }
    public function update($uid, $data)
    {
		header('Access-Control-Allow-Origin: *');	
		$userTable=$this->getServiceLocator()->get('Models\Model\UserFactory');
		$userDetailsTable=$this->getServiceLocator()->get('Models\Model\UserDetailsFactory');
		$userRegistration = $userTable->updateUserData($data,$uid);
		$userRegistrationDetails = $userDetailsTable->updateUserDetails($data,$uid);
		if($userRegistrationDetails){
			return new JsonModel(array(
				'output' => 'success',
				'value' => 1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'fail',
				'value' => 0,
			));
		}
    }
    public function delete($sub_id)
    {
       $subjectTable=$this->getServiceLocator()->get('Models\Model\SubjectsFactory');
		$deleteSubject = $subjectTable->deleteSubject($sub_id);
			if($deleteSubject!=0){
				return new JsonModel(array(
					'status'		=>	'success',
					'value'			=>	1,
				));	
			}else{
				return new JsonModel(array(
					'status'	=>	'fail',
					'value'		=>   0,
				));			
			}
    }
}